/**   
 * @Title: LoadDataTask.java
 * @Package com.bluemobi.diabetes.doctor.task
 * @Description: TODO
 * @author Evan 
 * @date Jun 16, 2013 10:40:20 PM 
 * @version V1.0   
 */
package com.evan.common.task;

import java.util.Map;

import org.json.JSONObject;

import android.content.Context;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import com.evan.common.connection.ConnectionCommon;
import com.evan.common.utils.CommonUtility;
import com.evan.common.widget.LoadingView;

public class RequestTask extends AsyncTask<Map<String, Object>, Void, Object> {

	private Context context;
	Handler handler;
	private String request;
	private boolean isShowDialog = true;
	public static final int JSON = 0x001, CHAR = 0x002;
	private int dataType = JSON;

	/**
	 * 
	 * @param context
	 * @param request
	 *            url path e.g (doctor_if.php)
	 * @param handler
	 * @param responseCode
	 *            process code for handler
	 */
	public RequestTask(Context context, String request, Handler handler) {
		this.context = context;
		this.request = request;
		this.handler = handler;
	}

	public RequestTask(Context context, String request, Handler handler,
			int dataType) {
		this.context = context;
		this.request = request;
		this.handler = handler;
		this.dataType = dataType;
	}

	public RequestTask(Context context, String request, Handler handler,
			int dataType, boolean isShowDialog) {
		this.context = context;
		this.request = request;
		this.handler = handler;
		this.dataType = dataType;
		this.isShowDialog = isShowDialog;
	}

	/**
	 * 
	 * @param context
	 * @param request
	 *            url path e.g (doctor_if.php)
	 * @param handler
	 * @param responseCode
	 *            process code for handler
	 */
	public RequestTask(Context context, String request, Handler handler,
			boolean isShowDialog) {
		this.context = context;
		this.request = request;
		this.handler = handler;
		this.isShowDialog = isShowDialog;
	}

	@Override
	protected Object doInBackground(Map<String, Object>... params) {
		try {
			Map<String, Object> param = null;
			if (!CommonUtility.isNull(params) && params.length > 0) {
				param = params[0];
			}
			String result = ConnectionCommon
					.getInstance()
					.sendRequestFromHttpClient(context, request, param, handler);
			if (dataType == JSON) {
				if (!CommonUtility.isNull(result)) {
					return new JSONObject(result);
				}
			} else {
				return result;
			}
		} catch (Exception e) {
			// TODO Auto-generated catch block
			
			e.printStackTrace();
		}
		return null;
	}

	@Override
	protected void onPostExecute(Object object) {
		// TODO Auto-generated method stub
		super.onPostExecute(object);
		if (handler != null) {
			Message msg = handler.obtainMessage();
			msg.obj = object;
			handler.sendMessage(msg);
		}
		if (isShowDialog)
//			LoadingProgressExt.dismiss();
			LoadingView.hide(context);
	}

	@Override
	protected void onPreExecute() {
		// TODO Auto-generated method stub
		if (isShowDialog)
//			LoadingProgressExt
//					.showProgressDialog(context, R.string.loading_txt);
			LoadingView.show(context);
		super.onPreExecute();
	}

}